package cn.wolfcode.wolf2w.advice;

import cn.wolfcode.wolf2w.exception.LogicException;

import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理
 */

// Controller增强
@ControllerAdvice
public class ExceptionHandle {

    /**
     * 捕获指定异常
     * @param e
     * @return
     */
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object logicException(RuntimeException e){
        e.printStackTrace();
        return new JsonResult<>(JsonResult.CODE_ERROR_PARAM,e.getMessage(),null);
    }

    /**
     * 捕获系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object handlerException(Exception e){
        e.printStackTrace();
        return JsonResult.defaultError();
    }
}
